package com.commerce.order.controller;


import com.commerce.common.entity.Cart;
import com.commerce.common.utils.Result;
import com.commerce.order.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 添加商品到购物车
     */
    @PostMapping
    public Result<Cart> addToCart(@RequestBody Cart cart) {
        Cart addedCart = cartService.addToCart(cart);
        return Result.success(addedCart);
    }

    /**
     * 更新购物车商品数量
     */
    @PutMapping("/{id}")
    public Result<Cart> updateCartQuantity(@PathVariable Long id, @RequestParam Integer quantity) {
        Cart updatedCart = cartService.updateCartQuantity(id, quantity);
        return Result.success(updatedCart);
    }

    /**
     * 删除购物车商品
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteCartItem(@PathVariable Long id) {
        cartService.removeById(id);
        return Result.success();
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("/user/{userId}")
    public Result<Void> clearCart(@PathVariable Long userId) {
        cartService.clearCart(userId);
        return Result.success();
    }

    /**
     * 获取用户购物车列表
     */
    @GetMapping("/user/{userId}")
    public Result<List<Cart>> getUserCart(@PathVariable Long userId) {
        List<Cart> cartList = cartService.getUserCart(userId);
        return Result.success(cartList);
    }

    /**
     * 获取购物车商品数量
     */
    @GetMapping("/count/{userId}")
    public Result<Integer> getCartCount(@PathVariable Long userId) {
        Integer count = cartService.getCartCount(userId);
        return Result.success(count);
    }

    /**
     * 检查商品是否在购物车中
     */
    @GetMapping("/check")
    public Result<Cart> checkProductInCart(@RequestParam Long userId, @RequestParam Long productId) {
        Cart cart = cartService.checkProductInCart(userId, productId);
        return Result.success(cart);
    }
} 